apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../pop"], function(V, $api, $, $app, service, $pop) {
        var vm = {
            el: '.container',
            data: {
                isShow: false,
                tabTxt: ['全部', '待付款', '待发货', '待收货', '待评价'],
                tabIndex: 0,
                list: [],
                order_state: 0,
                page: 1,
                cancelList: '',
                order_id: '',
                is_group: false,
                have_data: true,
                unionid: '',
                group_id: '',
                goods_title: '',
                goods_img: '',
                cancelIndex: -1,
                cancelChecked: -1,
                bs: "" //ajax弹出服务器提示控制变量
            },
            methods: {
                init: function(isNull) {
                    if ($api.getStorage("user")) {
                        // this.userInfo = $api.getStorage("user").data;
                        this.bs = 1;
                    }

                    if ($api.getStorage("shop_unionid")) {
                        this.unionid = $api.getStorage("shop_unionid");
                    }

                    if (this.have_data) {
                        this.getList(isNull);
                    } else {
                        $app.toast("没有更多数据了！");
                    }
                },
                //--获取订单列表order_state(0或空为全部，1待付款，2待发货，3待收货，4待评价，5已完成)
                getList: function(isNull) {
                    var self = this;
                    var _isNull = typeof isNull == 'boolean' ? isNull : false;
                    $app.ajax(service.buyGetOrderList, {
                        data: {
                            values: {
                                unionid: self.unionid,
                                order_state: self.order_state,
                                page: self.page
                            }
                        }
                    }, function(res) {
                        console.log("orderMy: " + JSON.stringify(res));
                        if (res.code == 0) {
                            if (_isNull) {
                                self.list = [];
                            }
                            if (res.data.length > 0 && Array.isArray(res.data)) {
                                var data = res.data;
                                for (var i = 0; i < data.length; i++) {
                                    self.list.push(data[i]);
                                }
                                self.page++;
                            }
                            document.querySelector("#orderMyNull").style.display = self.list.length <= 0 ? "block" : "none";
                            self.have_data = res.data.length <= 0 ? false : true;
                        }
                    });
                },
                //--tab切换
                orderMyTab: function(et) {
                    var el = et.currentTarget;
                    var index = +el.dataset.index;
                    this.tabIndex = index;
                    this.order_state = index;
                    this.page = 1;
                    this.have_data = true;
                    this.getList(true);
                },
                toShop:function(){
                  
                },
                //--去付款
                goPayMent: function(et) {
                    // if ($api.getStorage('Vstatus') == 0) {
                    if (this.bs == "") {
                        // $app.toast($api.getStorage('Vstatus'))
                        $app.toast("体验更多功能，请前往登录")
                    } else {
                        var pay_key = et.currentTarget.dataset.code;
                        var money = et.currentTarget.dataset.money;
                        var order_id = +et.currentTarget.parentNode.dataset.orderid;
                        var is_group = et.currentTarget.parentNode.dataset.group == "1" ? true : false;
                        var yzc_status = et.currentTarget.dataset.yzc_status;
                        if (yzc_status) {
                            $app.openWin("buyOrderPayWay", {
                                pay_key: pay_key,
                                money: money,
                                is_group: is_group,
                                pay_channel: 'yzc'
                            });
                        } else {
                            $app.openWin("buyOrderPayWay", {
                                pay_key: pay_key,
                                money: money,
                                is_group: is_group,
                            });
                        }

                    }

                },
                cancelSelect: function(et) {
                    this.cancelChecked = +et.currentTarget.dataset.index;
                },
                //--取消订单，选择原因
                cancelOrder: function(et) {
                    var self = this;
                    this.cancelChecked = -1;
                    this.cancelIndex = +et.currentTarget.dataset.index;
                    this.order_id = +et.currentTarget.parentNode.dataset.orderid;
                    this.is_group = et.currentTarget.parentNode.dataset.group == "1" ? true : false;
                    $app.ajax(service.getCancelReasonList, {
                        data: {
                            values: {
                                unionid: self.unionid,
                                bs: ''
                            }
                        }
                    }, function(res) {
                        console.log("cancelList: " + JSON.stringify(res));
                        if (res) {
                            if (Array.isArray(res)) {
                                self.cancelList = res;
                            } else {
                                self.cancelList = res.data;
                            }
                            self.$refs.orderQuestion.style.display = "block";
                        } else {
                            self.$refs.orderQuestion.style.display = "block";
                        }
                    });
                },
                //--取消订单，确认取消
                sureCanelOrder: function(et) {
                    var unionid = $api.getStorage("shop_unionid"),
                        self = this;
                    var openid = $api.getStorage("user").data.openid;
                    var order_reason_id = '',
                        other_reason = '';
                    var ul = this.$refs.orderQuestionUl.querySelectorAll("input");
                    for (var i = 0; i < ul.length; i++) {
                        if (ul[i].checked && i != ul.length - 1) {
                            order_reason_id = ul[i].dataset.id;
                            other_reason = ul[i].dataset.content;
                            break;
                        }
                        if (ul[i].checked && i == ul.length - 1) {
                            order_reason_id = ul[i].dataset.id;
                            other_reason = ul[i].parentNode.querySelectorAll("textarea")[0].value;
                        }
                    }
                    if (other_reason == '' || order_reason_id == '') {
                        api.toast({
                            msg: '请选择或输入其他原因',
                            duration: 2000,
                            location: 'middle'
                        });
                        return;
                    }
                    $app.ajax(service.buyCancelOrder, {
                        data: {
                            values: {
                                unionid: self.unionid,
                                openid: openid,
                                order_id: self.order_id,
                                order_reason_id: order_reason_id,
                                other_reason: other_reason
                            }
                        }
                    }, function(res) {
                        //cancelList: {"code":307,"message":"提交申请成功，请耐心等待","data":[]}
                        console.log("cancelList: " + JSON.stringify(res));
                        if (res.code == 0) {
                            self.closeCanelOrder();
                            $pop.tip("订单取消成功！");
                            self.list.splice(self.cancelIndex, 1);
                            //self.page = 1;
                            //self.init(true);
                        } else {
                            $pop.tip("订单取消失败！");
                        }
                    });
                },
                //--取消订单，关闭
                closeCanelOrder: function(et) {
                    this.$refs.orderQuestion.style.display = "none";
                },
                //--查看物流
                viewFlow: function(et) {
                    var order_id = +et.currentTarget.parentNode.dataset.orderid;
                    $app.openWin("buyViewFlow", {
                        order_id: order_id
                    })
                },
                //--提醒发货
                remindSend: function(et) {
                    var unionid = $api.getStorage("shop_unionid"),
                        self = this;
                    var order_id = +et.currentTarget.parentNode.dataset.orderid;
                    $app.ajax(service.buyReminderSlip, {
                        data: {
                            values: {
                                unionid: self.unionid,
                                order_id: order_id
                            }
                        }
                    }, function(res) {
                        //console.log(JSON.stringify(res));
                        if (res.code == 0) {
                            $pop.tip("已成功提醒卖家，会尽快为您发货！");
                        }
                    });
                },
                //--确认收货
                sureReceipt: function(et) {
                    var self = this;
                    var order_id = +et.currentTarget.parentNode.dataset.orderid;
                    var is_group = et.currentTarget.dataset.group == '1' ? true : false;
                    var btnStyle = ['', 'background: #fe830b;color: #fff;'];
                    if (is_group) {
                        btnStyle = ['', 'background: #f42020;color: #fff;'];
                    }
                    $pop.msg({
                        title: '确认收货',
                        msg: "是否收到货！",
                        btns: ['取消', "确认收货"],
                        btnStyle: btnStyle
                    }, function(res) {
                        if (res.index == 1) {
                            $app.ajax(service.buyOrderReceived, {
                                data: {
                                    values: {
                                        unionid: self.unionid,
                                        order_id: order_id
                                    }
                                }
                            }, function(res) {
                                if (res.code == 0) {
                                    $app.openWin("buyDealComplate", {
                                        is_group: is_group,
                                        order_id: order_id
                                    });
                                }
                            });
                        }
                    });
                },
                //--联系卖家
                contactSeller: function(et) {
                    var userinfo = JSON.parse(et.currentTarget.dataset.userinfo);
                    $app.yz_chat(userinfo);
                },
                //--再次购买
                buyAgain: function(et) {
                    var self = this;
                    var is_group = et.currentTarget.parentNode.dataset.group == '1' ? true : false;
                    var goods_id = [],
                        option_id = [],
                        quantity = [];
                    var index = et.currentTarget.dataset.index;
                    var yzc = et.currentTarget.dataset.yzc == '1' ? true : false;
                    var li = document.querySelectorAll("#orderMyList li")[+index].querySelectorAll(".order_list_box");
                    for (var i = 0; i < li.length; i++) {
                        goods_id.push(li[i].dataset.goodsid);
                        option_id.push(li[i].dataset.optionid);
                        quantity.push(li[i].dataset.quantity);
                    }
                    if (goods_id.length <= 0 || option_id.length <= 0 || quantity.length <= 0) {
                        return;
                    }
                    if (this.list[+index].options_has == -1) {
                        $pop.tip(this.list[+index].options_tip);
                        return;
                    }
                    if (yzc) {
                        var options = et.currentTarget.dataset.options;
                        $app.openWin('shopIndexYzcOrder', {
                            options: options
                        });
                        return;
                    }
                    if (is_group) {
                        var options = et.currentTarget.dataset.options;
                        if (options.indexOf("|") > -1) {
                            options = options.replace(/\|/) + ",0,0";
                        } else {
                            options = options + ',0,0';
                        }
                        $app.openWin("buyOrder", {
                            options: options
                        });
                        return;
                    }

                    againBuy(goods_id.length - 1);

                    function againBuy(index) {
                        if (index < 0) {
                            var btnStyle = ['', 'background: #fe830b;color: #fff;'];
                            if (is_group) {
                                btnStyle = ['', 'background: #f42020;color: #fff;'];
                            }
                            var msgTip = 0,
                                fail = 0,
                                tipMsg = '',
                                btns = ['取消', '前往购物车'];
                            // for (var i = 0; i < res.length; i++) {
                            //     if (res[i] == true) {
                            //         msgTip++;
                            //     } else {
                            //         tipMsg = res[i];
                            //         fail++;
                            //     }
                            // }
                            if (fail == 0) {
                                tipMsg = '商品加入购物车！';
                            } else if (msgTip == 0) {
                                btns = ['取消', '确定'];
                                $pop.tip(tipMsg);
                                return;
                            } else {
                                tipMsg = '加入购物车，' + msgTip + '成功，' + fail + '失败';
                            }
                            $pop.msg({
                                title: '温馨提示',
                                msg: tipMsg,
                                btns: btns,
                                btnStyle: btnStyle
                            }, function(res) {
                                if (res.index == 1 && msgTip != 0) {
                                    $app.openWin("buyCart");
                                }
                            })
                            return;
                        }
                        $app.ajax(service.addToCart, {
                            data: {
                                values: {
                                    unionid: self.unionid,
                                    goods_id: goods_id[index],
                                    option_id: option_id[index],
                                    quantity: quantity[index],
                                    mask: 'none',
                                    bs: ''
                                }
                            }
                        }, function(res) {
                            //console.log("buyAgain: "+JSON.stringify(res));
                            if (res.code == 0) {
                                index--;
                                againBuy(index);
                            } else {
                                $app.toast('加入购物车失败');
                            }
                        });

                    }





                },
                //--去评价
                buyComment: function(et) {
                    var order_id = et.currentTarget.parentNode.dataset.orderid;
                    var is_group = et.currentTarget.parentNode.dataset.group == '1' ? true : false;
                    var index = +et.currentTarget.dataset.index;
                    var goods = this.$refs.orderMyList.querySelectorAll("li")[index].querySelectorAll(".order_list_box");
                    var goods_id = [];
                    for (var i = 0; i < goods.length; i++) {
                        goods_id.push(goods[i].dataset.goodsid);
                    }
                    $app.openWin('buyCommentList', {
                        order_id: order_id,
                        is_group: is_group
                    });
                },
                //--去订单详情
                goOrderDetail: function(et) {
                    var order_id = et.currentTarget.dataset.orderid;
                    var is_group = et.currentTarget.dataset.group == '1' ? true : false;
                    $app.openWin('buyOrderDetail', {
                        order_id: order_id,
                        is_group: is_group
                    });
                },
                //--删除订单
                orderDelete: function(et) {
                    var self = this;
                    var order_id = +et.currentTarget.parentNode.dataset.orderid;
                    var index = +et.currentTarget.dataset.index;
                    var is_group = et.currentTarget.dataset.group == '1' ? true : false;
                    var btnStyle = ['', 'background: #fe830b;color: #fff;'];
                    if (is_group) {
                        btnStyle = ['', 'background: #f42020;color: #fff;'];
                    }
                    $pop.msg({
                        title: '温馨提示',
                        msg: '确认要删除订单吗？',
                        btns: ['取消', '删除'],
                        btnStyle: btnStyle
                    }, function(res) {
                        if (res.index == 1) {
                            $app.ajax(service.buyDelOrder, {
                                data: {
                                    values: {
                                        unionid: self.unionid,
                                        order_id: order_id
                                    }
                                }
                            }, function(res) {
                                //console.log("buyDelOrder: "+JSON.stringify(res));
                                if (res.code == 0) {
                                    //self.list.splice(index,1);
                                    $pop.tip("订单删除成功！");
                                    self.have_data = true;
                                    self.page = 1;
                                    self.init(true);
                                } else {
                                    $pop.tip("删除失败！");
                                }
                            });
                        }
                    });
                },
                //--延长收货
                delayReceipt: function(et) {
                    var self = this;
                    var order_id = +et.currentTarget.parentNode.dataset.orderid;
                    $app.ajax(service.buySetCustomerDelay, {
                        data: {
                            values: {
                                unionid: self.unionid,
                                order_id: order_id
                            }
                        }
                    }, function(res) {
                        //console.log("buySetCustomerDelay: "+JSON.stringify(res));
                        if (res.code == 0) {
                            api.toast({
                                msg: '设置成功，已设置延长收货',
                                duration: 2000,
                                location: 'middle'
                            });
                        }
                    });
                },
                //--退款申请
                goRefundApply: function(et) {
                    var order_id = et.currentTarget.parentNode.dataset.orderid;
                    var is_group = et.currentTarget.parentNode.dataset.group == '1' ? true : false;
                    $app.openWin('buyRefundServer', {
                        order_id: order_id,
                        is_group: is_group
                    });
                },
                //--退款详情
                goRefundDetail: function(et) {
                    var order_id = et.currentTarget.parentNode.dataset.orderid;
                    var is_group = et.currentTarget.parentNode.dataset.group == '1' ? true : false;
                    var order_return_id = et.currentTarget.dataset.id;
                    $app.openWin("buyRefundDetail", {
                        order_return_id: order_return_id,
                        order_id: order_id
                    });
                },
                //--撤销申请
                orderRepealRefund: function(et) {
                    var self = this;
                    var is_group = et.currentTarget.parentNode.dataset.group == '1' ? true : false;
                    var order_return_id = et.currentTarget.dataset.id;
                    var btnStyle = ['', 'background: #fe830b;color: #fff;'];
                    if (is_group) {
                        btnStyle = ['', 'background: #f42020;color: #fff;'];
                    }
                    $pop.msg({
                        title: '温馨提示',
                        msg: '确认要撤销退款申请吗？',
                        btns: ['取消', '撤销'],
                        btnStyle: btnStyle
                    }, function(res) {
                        if (res.index == 1) {
                            $app.ajax(service.buyUndoRefund, {
                                data: {
                                    values: {
                                        unionid: self.unionid,
                                        order_return_id: order_return_id
                                    }
                                }
                            }, function(res) {
                                //console.log("buyDelOrder: "+JSON.stringify(res));
                                if (res.code == 0) {
                                    $pop.tip("退款，撤销申请成功！");
                                    self.have_data = true;
                                    self.page = 1;
                                    self.init(true);
                                } else {
                                    $pop.tip("撤销失败！");
                                }
                            });
                        }
                    });
                },
                //--打开分享
                openShare: function(et) {
                    this.group_id = +et.currentTarget.dataset.groupid;
                    this.goods_title = et.currentTarget.dataset.name;
                    this.goods_img = et.currentTarget.dataset.img;
                    this.$refs.orderSareGroup.style.display = 'block';
                },
                closeShare: function(et) {
                    this.$refs.orderSareGroup.style.display = 'none';
                },
                // 分享到微信
                shareToWx: function(et) {
                    var self = this;
                    var wx = api.require('wx');
                    var toWhere = '';
                    var type = +et.currentTarget.dataset.type;
                    if (type == 1) {
                        // 分享给朋友
                        toWhere = 'session';
                    } else if (type == 2) {
                        // 分享到朋友圈
                        toWhere = 'timeline';
                    }
                    if (self.group_id == undefined || self.group_id == '') {
                        return;
                    }
                    $app.ajax(service['Share'], {
                        data: {
                            values: {
                                type: 3,
                                unionid: $api.getStorage("shop_unionid"),
                                share_id: self.group_id
                            }
                        }
                    }, function(res) {
                        if (res.code == 0) {
                            var url = res.data;
                            $app.downloadImg(self.goods_img, function(img_url) {
                                wx.shareWebpage({
                                    scene: toWhere,
                                    title: self.goods_title,
                                    description: '您的好友向您分享一个拼团邀请，打开连接即可查看',
                                    contentUrl: url,
                                    thumb: img_url
                                }, function(ret, err) {
                                    if (ret.status) {
                                        $app.toast('分享成功');
                                        self.closeShare();
                                    } else {
                                        $app.toast('分享失败');
                                    }
                                });
                            });
                        } else {
                            api.toast({
                                msg: '获取分享链接失败',
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    });
                }
            },
            mounted: function() {
                var self = this;
                document.querySelector("#orderSearch").addEventListener("click", function(et) {
                    $app.openWin('buyOrderSearch');
                });
                //--取消订单回来，刷新列表
                api.addEventListener({
                    name: 'orderCanelBackOrderMy_'
                }, function(ret, err) {
                    self.init();
                });
                //--删除订单回来，刷新列表
                api.addEventListener({
                    name: 'delBackOrderMy_'
                }, function(ret, err) {
                    self.have_data = true;
                    self.page = 1;
                    self.init(true);
                });
                //--撤销申请，返回列表
                api.addEventListener({
                    name: 'refundRepeal_'
                }, function(ret, err) {
                    self.have_data = true;
                    self.page = 1;
                    self.init(true);
                });
                //--交易完成
                api.addEventListener({
                    name: 'orderInit_'
                }, function() {
                    self.have_data = true;
                    self.page = 1;
                    self.init(true);
                });

                //--返回个人中心
                function btnKeyBack() {
                    api.sendEvent({
                        name: 'orderBackperson_'
                    });
                    $api.setTimeout(function() {
                        api.closeToWin({
                            name: api.pageParam.name || api.pageParam.prve
                        });
                    }, 300);
                }
                //--点击上面按钮返回
                $api.addEvt($api.dom('#btn-back'), 'click', function() {
                    btnKeyBack();
                });

                //--返回键
                api.addEventListener({
                    name: 'keyback'
                }, function(ret, err) {
                    btnKeyBack();
                });

                //--加载更多
                api.addEventListener({
                    name: 'scrolltobottom',
                    extra: {
                        threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
                    }
                }, function(ret, err) {
                    self.init();
                });
            },
            created: function() {
                this.order_state = api.pageParam.index;
                this.tabIndex = api.pageParam.index;
                this.page = 1;
                this.init();
            }
        };
        //var shopVue = new Vue(vm);
        var even = function() {

        };
        var newVue = new Vue(vm);
        even();

    })
}
